package com.zte.smbms_plus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zte.smbms_plus.pojo.SmbmsRole;
import com.zte.smbms_plus.pojo.SmbmsUser;
import com.zte.smbms_plus.service.SmbmsRoleService;
import com.zte.smbms_plus.service.SmbmsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/user/")
public class SmbmsUserController {
    @Autowired
    private SmbmsUserService smbmsUserService;

    @Autowired
    private SmbmsRoleService smbmsRoleService;

    @RequestMapping("login")
    public String login(SmbmsUser smbmsUser,Model model) {
        QueryWrapper<SmbmsUser> smbmsUserQueryWrapper = new QueryWrapper<>();
        smbmsUserQueryWrapper.eq("userpassword",smbmsUser.getUserpassword());
        smbmsUserQueryWrapper.eq("usercode",smbmsUser.getUsercode());
        SmbmsUser one = smbmsUserService.getOne(smbmsUserQueryWrapper);
        if (one != null) {
            return "redirect:list/1";
        }
        model.addAttribute("msg","用户名或者密码错误！");
        return "login.html";
    }

    @RequestMapping("list/{currentPage}")
    public String list(@PathVariable Long currentPage, Model model,String queryname,String queryUserRole) {
        QueryWrapper<SmbmsUser> smbmsUserQueryWrapper = new QueryWrapper<>();
        if (queryname != null && !"".equals(queryname)) {
            smbmsUserQueryWrapper.like("username",queryname);
            model.addAttribute("queryname",queryname);
        }
        if (queryUserRole != null && !"".equals(queryUserRole) && !"0".equals(queryUserRole)) {
            model.addAttribute("queryUserRole",queryUserRole);
            smbmsUserQueryWrapper.like("userrole",queryUserRole);
        }
        Page<SmbmsUser> smbmsUserPage = new Page<>(currentPage, 4);
        smbmsUserService.page(smbmsUserPage,smbmsUserQueryWrapper);
        model.addAttribute("userPage",smbmsUserPage);

        return "userlist";
    }
    
    @RequestMapping("infos/{op}/{current}/{id}")
    public String show(@PathVariable String op,@PathVariable Long id,Model model,@PathVariable Long current) {
        SmbmsUser byId = smbmsUserService.getById(id);
        model.addAttribute("user",byId);
        if ("show".equals(op)) {
            return "userview";
        }

        List<SmbmsRole> rolelist = smbmsRoleService.list();
        model.addAttribute("roles",rolelist);
        return "usermodify";
    }

    @RequestMapping("update")
    public String update(SmbmsUser smbmsUser,Model model,Long current) {
        if (smbmsUserService.updateById(smbmsUser)) {
            model.addAttribute("msg","修改成功！！");
        }else {
            model.addAttribute("msg","修改失败！！！！");
        }

        return "forward:list/" + current;
    }

    @RequestMapping("add")
    public String add(SmbmsUser smbmsUser,Model model) {
        if (smbmsUserService.save(smbmsUser)) {
            model.addAttribute("msg","增加成功");
        }else {
            model.addAttribute("msg","增加失败！！");
        }

        return "forward:list/1";
    }

    @RequestMapping("delete")
    public String delete(Long current,Long id,Model model) {
        if (smbmsUserService.removeById(id)) {
            model.addAttribute("msg","删除成功！！");
        }else {
            model.addAttribute("msg","删除失败！！");
        }

        return "forward:list/" + current;
    }
}
